/**
 * 角色管理 - 选择用户
 * @author yuan_rz
 * @date 2019/01/04 15:30
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            form: ''
        },
        callObj,
        usersData = null,
        data = null,
        table
    ;

    var RoleUsers = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'role_users',
            }, win);
        },
        users: function (fn) {
            app.ajax({
                xurl: 'sys/role/getUsersList',
                cb: function (result) {
                    if (result.ok) {
                        usersData = result.data.data;
                        fn && fn();
                    }
                }
            });
        },
        initEnableUsersTable: function () {
            table.render({
                elem: app.getByid(doc, 'enable_users_table'),
                id: 'enable_users_table',
                cols: [[
                    {field: 'ID', type: 'checkbox', title: '选择'},
                    {field: 'NAME', width: 100, title: '用户名称'},
                    {field: 'REMARK', minWidth: 150, title: '备注'}
                ]],
                page: {layout: ['prev', 'page', 'next', 'count']}
            });
        },
        initSelectedUsersTable: function () {
            table.render({
                elem: app.getByid(doc, 'selected_users_table'),
                id: 'selected_users_table',
                size: _layuiConfig.ui_size,
                cols: [[
                    {field: 'ID', type: 'checkbox', title: '选择'},
                    {field: 'NAME', width: 100, title: '用户名称'},
                    {field: 'REMARK', title: '备注'}
                ]],
                page: {layout: ['prev', 'page', 'next', 'count']}
            });
        },
        tableReload: function () {
            table.reload('enable_users_table', {
                data: data.filterKV({has: 0})
            });
            table.reload('selected_users_table', {
                data: data.filterKV({has: 1})
            });
        },
        usersByRoleId: function () {
            data = usersData;
            data.forEach(function (v) {
                if (v.TYPE != callObj.data.TYPE) {
                    data.removeByMap({
                        TYPE: v.TYPE
                    });
                }
            });
            data.addMap({has: 0}, true);
            app.ajax({
                xurl: 'sys/role/getRoleUsersList',
                xdata: {
                    roleId: callObj.data.ID
                },
                cb: function (result) {
                    if (result.ok) {
                        result.data.data.forEach(function (v) {
                            data.filterKV({ID: v.USER_ID}).addMap({has: 1}, true);
                        });
                        self.tableReload();
                    }
                }
            });
        },
        _call: function (obj) {
            callObj = obj;
            self.users();
            if (!usersData) {
                self.users(function () {
                    self.usersByRoleId();
                });
            } else {
                self.usersByRoleId();
            }
        },
        _ready: function () {
            self.users();
            table = layui.table;
            new Vue({
                el: '#role_users',
                data: vueData,
                methods: {
                    addUser: function () {
                        var checkStatus = table.checkStatus('enable_users_table');
                        if (checkStatus.data.length > 0) {
                            var _xdata = [];
                            checkStatus.data.forEach(function (v) {
                                var _temp = app.fields_('sys_user_role');
                                _temp.ROLE_ID = callObj.data.ID;
                                _temp.USER_ID = v.ID;
                                _xdata.push(_temp);
                            });
                            app.ajax({
                                xurl: 'sys/role/addRoleUsers',
                                xdata: _xdata,
                                loading: true,
                                toJsonStr: true,
                                okmsg: '用户角色添加成功',
                                cb: function (result) {
                                    if (result.ok) {
                                        result.xdata.forEach(function (v) {
                                            data.filterKV({ID: v.USER_ID}).addMap({has: 1}, true);
                                        });
                                        self.tableReload();
                                    }
                                }
                            });
                        }
                    },
                    deleteUser: function () {
                        var checkStatus = table.checkStatus('selected_users_table');
                        if (checkStatus.data.length > 0) {
                            var _xdata = [];
                            checkStatus.data.forEach(function (v) {
                                var _temp = app.fields_('sys_user_role');
                                _temp.ROLE_ID = callObj.data.ID;
                                _temp.USER_ID = v.ID;
                                _xdata.push(_temp);
                            });
                            app.ajax({
                                xurl: 'sys/role/deleteRoleUsers',
                                xdata: _xdata,
                                loading: true,
                                toJsonStr: true,
                                okmsg: '用户角色移除成功',
                                cb: function (result) {
                                    if (result.ok) {
                                        result.xdata.forEach(function (v) {
                                            data.filterKV({ID: v.USER_ID}).addMap({has: 0}, true);
                                        });
                                        self.tableReload();
                                    }
                                }
                            });
                        }
                    },
                    sure: function () {

                    }
                },
                watch: {},
                created: function () {
                    self._setEleIsReady('role_users')
                }
            });
            self.initEnableUsersTable();
            self.initSelectedUsersTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new RoleUsers();

});